package star.cloud.asme.system.permission;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;
import star.cloud.asme.system.role.RoleService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class PermissionServiceImpl extends IBaseServiceImpl<PermissionMapper, Permission> implements PermissionService {

    private final PermissionMapper permissionMapper;
    private final RoleService roleService;

    @Override
    public Result<Permission> insertOne(Permission entity) {
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getText, entity.getText());
        queryWrapper.eq(Permission::getMenuTitle, entity.getMenuTitle());
        List<Permission> permissions = permissionMapper.selectList(queryWrapper);
        if (permissions.isEmpty()) {
            permissionMapper.insert(entity);
            return Result.ok(entity);
        } else {
            return Result.error(entity).msg("按钮标签已经存在，请修改后再提交。");
        }
    }

    @Override
    public Result<Permission> updateOne(Permission entity) {
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getText, entity.getText());
        queryWrapper.eq(Permission::getMenuTitle, entity.getMenuTitle());
        List<Permission> permissions = permissionMapper.selectList(queryWrapper);
        if (permissions.isEmpty()) {
            setVersion(entity);
            permissionMapper.updateById(entity);
            return Result.ok(entity);
        } else {
            return Result.error(entity).msg("按钮标签已经存在，请修改后再提交。");
        }
    }

    @Override
    public Result<List<Permission>> getListByConditions(Permission entity) {
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getMenuTitle, entity.getMenuTitle());
        List<Permission> permissions = permissionMapper.selectList(queryWrapper);
        permissions.forEach(this::setCreatorAndUpdater);
        return Result.ok(permissions);
    }

    @Override
    public Result<?> resetPermissionListByRoleAndMenu(PermissionDTO permissionDTO) {
        String roleName = permissionDTO.getRoleName();
        String menuTitle = permissionDTO.getMenuTitle();
        List<String> permissionTextList = permissionDTO.getButtonTextList();
        permissionMapper.deleteByRoleNameAndMenuTitle(roleName, menuTitle);
        if (permissionTextList != null) {
            for (String buttonText : permissionTextList) {
                permissionMapper.insertRoleAndMenuAndButton(roleName, menuTitle, buttonText);
            }
        }
        return Result.ok();
    }

    @Override
    public Result<List<String>> selectPermissionTextListByRoleNameAndMenuTitle(PermissionDTO permissionDTO) {
        List<String> permissionTextList = permissionMapper.selectPermissionTextListByRoleNameAndMenuTitle(permissionDTO.getRoleName(), permissionDTO.getMenuTitle());
        return Result.ok(permissionTextList);
    }

    @Override
    public Result<List<Permission>> selectPermissionListByRoleNameAndMenuTitle(PermissionDTO permissionDTO) {
        List<String> buttonTextList = permissionMapper.selectPermissionTextListByRoleNameAndMenuTitle(permissionDTO.getRoleName(), permissionDTO.getMenuTitle());
        List<Permission> permissions = new ArrayList<>();
        if (!buttonTextList.isEmpty()) {
            LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Permission::getText, buttonTextList);
            permissions = permissionMapper.selectList(lambdaQueryWrapper);
        }
        permissions.forEach(this::setCreatorAndUpdater);
        return Result.ok(permissions);
    }

    @Override
    public void deleteByRoleNameAndMenuTitles(String roleName, List<String> menuTitles) {
        if (menuTitles != null && !menuTitles.isEmpty()) {
            menuTitles.forEach(menuTitle -> permissionMapper.deleteByRoleNameMenuTitle(roleName, menuTitle));
        }
    }

    @Override
    public void myDeleteByRoleName(String roleName) {
        permissionMapper.deleteByRoleName(roleName);
    }

    @Override
    public Result<List<String>> getPermissionTextListByUserId(Long userId) {
        List<String> permissionTextList = permissionMapper.selectPermissionTextListByUserId(userId);
        List<String> roleNameList = roleService.getRoleNameListByUserId(userId);
        roleNameList.stream().filter(Objects::nonNull).distinct().map(item -> "ROLE_" + item).forEach(permissionTextList::add);
        return Result.ok(permissionTextList.stream().filter(Objects::nonNull).distinct().toList());
    }
}